import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:pcp/main.dart';
import 'package:pcp/pages/register_page.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pcp/common/config/config.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginState createState() => new _LoginState();
}

class _LoginState extends State<LoginPage> {
  TextEditingController _mailController = new TextEditingController();
  TextEditingController _pwdController = new TextEditingController();
  // var userId = "";
  // var userName = "";
  // var user = {};

  @override
  Widget build(BuildContext context) {
    final logo = Text(
      "PCP",
      textAlign: TextAlign.center,
      style: new TextStyle(
        color: Colors.blue,
        fontSize: 50.0,
        fontWeight: FontWeight.bold,
      ),
    );

    final mail = TextFormField(
      keyboardType: TextInputType.emailAddress,
      autofocus: false,
      //initialValue: '15688839826',
      controller: _mailController,
      decoration: InputDecoration(
          hintText: 'email',
          contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
          border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
    );

    final password = TextFormField(
      autofocus: false,
      //initialValue: '请输入密码',
      obscureText: true,
      controller: _pwdController,
      decoration: InputDecoration(
          hintText: 'Password',
          contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
          border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
    );

    final loginButton = Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        borderRadius: BorderRadius.circular(30.0),
        shadowColor: Colors.lightBlueAccent.shade100,
        elevation: 5.0,
        child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: () async {
            try {
              Response response = await Dio().post(
                  Config.BASE_URL + "/user/findUser",
                  data: {
                    "username" : "zy",
                    "password" : "",
                    "code" : "",
                  }
              );
              // user = response.data;
              // print(user['userId']);
              // userId = response.data.userId;
              // userName = response.data.username;
              if(response.data != "密码错误" && response.data != "该邮箱未注册") {
                SharedPreferences prefs = await SharedPreferences.getInstance();
                await prefs.setString('userId', response.data['_id']);
                await prefs.setString('userName', response.data['username']);
                showToast(response.data['alias'] + "，欢迎回来！");
                Navigator.of(context).push(new MaterialPageRoute(
                    builder: (BuildContext context) => new MainPage(userName: response.data['username'],)));
              } else
                showToast(response.data);
            } catch (e) {
              print(e);
            }
          },
          color: Colors.green,
          child: Text(
            '登 录',
            style: TextStyle(color: Colors.white, fontSize: 20.0),
          ),
        ),
      ),
    );

    final forgotLabel = FlatButton(
      child: Text(
        '忘记密码?',
        style: TextStyle(color: Colors.black54, fontSize: 18.0),
      ),
      onPressed: () {},
    );


    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        leading: IconButton(
          icon: Icon(
            Icons.keyboard_arrow_left,
            color: Colors.black,
          ),
          onPressed: () => Navigator.of(context).pop(),
        ),
        elevation: 0,
        actions: [
          MaterialButton(
            child: Text("注册"),
              textColor: Colors.black,
              onPressed: () {
                Navigator.of(context).push(new MaterialPageRoute(
                    builder: (BuildContext context) => new RegisterPage()));
              }
          )
        ],
      ),
      backgroundColor: Colors.white,
      body: Center(
        child: Container(
          child: ListView(
            shrinkWrap: true,
            padding: EdgeInsets.only(left: 24.0, right: 24.0),
            children: <Widget>[
              logo,
              SizedBox(
                height: 48.0,
              ),
              mail,
              SizedBox(
                height: 8.0,
              ),
              password,
              SizedBox(
                height: 24.0,
              ),
              loginButton,
              forgotLabel
            ],
          ),
        ),
      ),
    );
  }
}
